package ro.gateway.aida.utils;

import java.io.StringWriter;
import java.util.ArrayList;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: CSVParser.java,v 1.1 2004/10/24 23:37:20 mihaipostelnicu Exp $
 */
public class CSVParser
{
      public static String encode( String[] items )
      {
            if ( items == null ) return null;

            StringBuffer sb = new StringBuffer();
            for ( int i = 0; i < items.length; i++ )
            {
                  if ( items[ i ] == null ) continue;

                  if ( ( items[ i ].indexOf( '"' ) != -1 ) || ( items[ i ].indexOf( ',' ) != -1 ) )
                  {
                        String item = items[ i ].replaceAll( "\"", "\"\"" );
                        sb.append( '"' ).
                              append( item ).
                              append( '"' );
                  }
                  else
                  {
                        sb.append( items[ i ] );
                  }
                  sb.append( "," );
            }

            return sb.toString();
      }

      public static String[] decode( String line )
      {
            if ( line == null || line.length() < 1 ) return null;
            line = line.trim();
            if ( line.length() < 1 ) return null;

            char[] chrs = line.toCharArray();

            int len = 0;
            ArrayList items = new ArrayList();
            String[] result = null;

            int i0 = 0;
            while ( true )
            {
                  // gineste urmatorul item
                  boolean escaped = false;
                  // item escapuit? adiklea intre " ?
                  int countGhili = 0;
                  while ( ( i0 + countGhili < chrs.length ) && ( chrs[ i0 + countGhili ] == '"' ) )
                        countGhili++;
                  if ( countGhili % 2 == 1 )
                  {
                        escaped = true;
                        i0++;
                  }

                  if ( escaped )
                  {
                        StringWriter item = new StringWriter( 25 );
                        while ( true )
                        {
                              if ( chrs[ i0 ] == '"' )
                              {
                                    countGhili = 0;
                                    while ( ( i0 + countGhili < chrs.length ) && ( chrs[ i0 + countGhili ] == '"' ) )
                                          countGhili++;
                                    if ( countGhili % 2 == 1 )
                                    {
                                          // sfarsit de item
                                          i0 += countGhili + 1;
                                          countGhili /= 2;
                                          for ( int i = 0; i < countGhili; i++ ) item.write( '"' );
                                          break;
                                    }
                                    else
                                    {
                                          i0 += countGhili;
                                          countGhili /= 2;
                                          for ( int i = 0; i < countGhili; i++ ) item.write( '"' );
                                    }
                              }
                              else
                              {
                                    int partEndIndex = i0;
                                    while ( ( partEndIndex < chrs.length ) && ( chrs[ partEndIndex ] != '"' ) )
                                    {
                                          partEndIndex++;
                                    }
                                    if ( partEndIndex >= chrs.length )
                                    {
                                          System.err.println( "horoare la col:" + i0 );
                                          break;
                                    }
                                    if ( chrs[ partEndIndex ] == '"' )
                                    {
                                          item.write( chrs, i0, partEndIndex - i0 );
                                          i0 = partEndIndex;
                                    }
                                    else
                                    {
                                          item.write( chrs, i0, partEndIndex - i0 + 1 );
                                          // last item
                                          i0 = chrs.length;
                                          break;
                                    }
                              }
                              if ( i0 >= chrs.length ) break;
                        }
                        items.add( item.toString() );
                  }
                  else
                  {
                        int endIndex = i0;
                        while ( ( endIndex < chrs.length - 1 ) && ( chrs[ endIndex ] != ',' ) )
                              endIndex++;
                        if ( chrs[ endIndex ] == ',' )
                        {
                              items.add( new String( chrs, i0, endIndex - i0 ) );
                              i0 = endIndex + 1;
                        }
                        else
                        {
                              // last item
                              items.add( new String( chrs, i0, endIndex - i0 + 1 ) );
                              i0 = chrs.length;
                        }
                  }

                  if ( i0 >= chrs.length ) break;
            }


            if ( items.size() > 0 )
            {
                  result = new String[ items.size() ];
                  items.toArray( result );
            }

            return result;
      }

      public static void main( String[] agrs )
      {
            System.out.println( System.currentTimeMillis() );
            printItems( CSVParser.decode( CSVParser.encode( new String[]{
                  "d\",,,uda", ",,cu\"cu,,", "\"\"\"muie\"\"\"\""
            } ) ) );
            System.out.println( System.currentTimeMillis() );
      }

      public static void printItems( String[] items )
      {
            if ( items != null )
            {
                  for ( int i = 0; i < items.length; i++ )
                  {
                        System.out.print( items[ i ] );
                        if ( i < items.length - 1 )
                        {
                              System.out.print( ", " );
                        }
                        else
                        {
                              System.out.println( "" );
                        }
                  }
            }
      }

}